package cn.sunshine.ymcc.mq.consumer;

import cn.sunshine.ymcc.dto.PayAliNotifyDto;
import cn.sunshine.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup="alipay-order-consumer",//消费者组
        topic="paynofity-topic",//topic
        selectorExpression="aliay-tags",//tags
        messageModel= MessageModel.BROADCASTING//广播模式，一个消息可被多个消费者消费

)
public class ConsumerOrderPayRollback implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;

    /**
     * 消费者只能成功消费一次
     * 这个方法只要不报错，就是自动签收，报错就还可以继续消费

     */
    @Override
    public void onMessage(MessageExt messageExt) {
        String message = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        PayAliNotifyDto notifyDto = JSONObject.parseObject(message, PayAliNotifyDto.class);
       // 修改订单状态
        courseOrderService.upDateByNotify(notifyDto);
    }
}
